var UserData = require('UserData')
var resLoader = require('resLoader')
var COMMON_VALUE =require('Common')
var goods = require('goods')
cc.Class({
    extends: cc.Component,
    properties: {
        recipePrefab:{
            default:null,
            type:cc.Prefab,
        }
    },

    // LIFE-CYCLE CALLBACKS:
    // onLoad () {},
 
    start () {
        this.UserData = UserData
        this.recipeList = this.node.getChildByName("滚动面板").getChildByName("view").getChildByName("Recipe List")

        this.moneyBlock = this.recipeList.getChildByName("Money Upgrade List").getChildByName("Upgrade Block Prefab")
        this.moneyLogo = this.moneyBlock.getChildByName("logo")
        this.moneyGainLabel = this.moneyBlock.getChildByName("Gain Label")
        this.reputationLogo = this.recipeList.getChildByName("Reputation Upgrade List").getChildByName("Upgrade Block Prefab").getChildByName("logo")
        this.reputationBlock = this.recipeList.getChildByName("Reputation Upgrade List").getChildByName("Upgrade Block Prefab")
        this.reputationGainLabel = this.reputationBlock.getChildByName("Gain Label")

        this.loadMoneyBuyingList(UserData.state.moneyBuyListOrder)
        this.loadReputationBuyList(UserData.state.reputationBuyListOrder)
        UserData.runtimeState.buttonUpdateList.push([this.checkButtonState,this])
        UserData.updateButtonList()
        this.checkButtonState(this)
        
    },
    
    loadMoneyBuyingList(order) {//界面中载入一个购买选项
        if(!order){
            order = 0
        }else if(order>COMMON_VALUE.MONEY_BUYING_LIST.length-1){
            order = COMMON_VALUE.MONEY_BUYING_LIST.length-1
        }
        this.itemLeft = COMMON_VALUE.MONEY_BUYING_LIST[order]
        let sprite = this.moneyLogo.getComponent(cc.Sprite)
        resLoader.plistLoader("Receipe/食材" ,this.itemLeft.id.toString() ,sprite)
        this.moneyBlock.getChildByName('Cost Label').getComponent(cc.Label).setNumber(this.itemLeft.cost)
        let content = null
        switch(this.itemLeft.target){
            case "ALL":
                content = "全部收益X"+this.itemLeft.times
            break
            case "YELLOW":
                content = "黄色餐厅的收益X"+this.itemLeft.times
            break
            case "BLUE":
                content = "蓝色餐厅的收益X"+this.itemLeft.times
            break
            case "RED":
                content = "红色餐厅的收益X"+this.itemLeft.times
            break
            default:
                content = "第"+ this.itemLeft.target +"层餐厅的收益X"+this.itemLeft.times
        }
        this.moneyGainLabel.getComponent(cc.Label).string = content

    },

    buyWithMoney(target){//购买按钮触发
        this.leftOrder = UserData.state.moneyBuyListOrder
        this.moneyLogo.getComponent(cc.Button).interactable = false
        let item = COMMON_VALUE.MONEY_BUYING_LIST[this.leftOrder]
        if(item){
            if(item.cost<UserData.getCurrentMoney()){
                UserData.state.moneyBuffList.push(item)
                UserData.state.moneyBuyListOrder += 1
                UserData.subCurrentMoney(item.cost)
                //远程扣钱
                goods.buyGood ({coins:item.cost}, item.id)

                this.moneyLogo.runAction(cc.sequence(
                    cc.scaleTo(0.3,0,0).easing(cc.easeBackIn()),
                    cc.callFunc((self)=>{
                        let rpm = self.parent.parent.parent.parent.parent.parent.getComponent('RecipeMenu')
                        rpm.loadMoneyBuyingList(rpm.leftOrder +1)
                        rpm.checkButtonState(rpm)
                    },this),
                    
                    cc.spawn(
                        cc.scaleTo(0.4,1,1).easing(cc.easeBackOut()),
                        cc.sequence(
                            cc.rotateTo(0.2,-10).easing(cc.easeSineOut()),
                            cc.rotateTo(0.1,10).easing(cc.easeSineInOut()),
                            cc.rotateTo(0.1,0).easing(cc.easeSineIn())
                        )
                    )
                ))
            }
        }
    },
    
    checkButtonState(self){//同时检查两个
        if(self.itemLeft.cost<UserData.getCurrentMoney()){
            self.moneyLogo.getComponent(cc.Button).interactable = true
        }else{
            self.moneyLogo.getComponent(cc.Button).interactable = false
        }
        if(self.itemRight.cost<UserData.getReputation()){
            self.reputationLogo.getComponent(cc.Button).interactable = true
        }else{
            self.reputationLogo.getComponent(cc.Button).interactable = false
        }
        return false
    },

    //声望动作
    loadReputationBuyList(order){
        if(!order){
            order = 0
        }else if(order>COMMON_VALUE.REPUTATION_BUYING_LIST.length-1){
            order = COMMON_VALUE.REPUTATION_BUYING_LIST.length-1
        }
        this.itemRight = COMMON_VALUE.REPUTATION_BUYING_LIST[order]
        let sprite = this.reputationLogo.getComponent(cc.Sprite)
        resLoader.plistLoader("Receipe/食谱" ,this.itemRight.id.toString(),sprite)
        this.reputationBlock.getChildByName('Cost Label').getComponent(cc.Label).setNumber(this.itemRight.cost)
        let content = null
        switch(this.itemLeft.target){
            case "ALL":
                content = "全部收益X"+this.itemLeft.times
            break
            case "YELLOW":
                content = "黄色餐厅的收益X"+this.itemLeft.times
            break
            case "BLUE":
                content = "蓝色餐厅的收益X"+this.itemLeft.times
            break
            case "RED":
                content = "红色餐厅的收益X"+this.itemLeft.times
            break
            default:
                content = "第"+ this.itemLeft.target +"层餐厅的收益X"+this.itemLeft.times
        }
        this.reputationGainLabel.getComponent(cc.Label).string = content
    },

    buyWithReputation(target){
        this.rightOrder = UserData.state.reputationBuyListOrder
        let item = COMMON_VALUE.REPUTATION_BUYING_LIST[this.rightOrder]
        this.reputationLogo.getComponent(cc.Button).interactable = false
        if(item){
            if(item.cost<UserData.getReputation()){
                UserData.state.reputationBuffList.push(item)
                UserData.state.reputationBuyListOrder += 1
                UserData.subReputation(item.cost)
                //更新远程声望
                goods.buyGood ({reputations:item.cost}, item.id)
                
                this.reputationLogo.runAction(cc.sequence(
                    cc.scaleTo(0.3,0,0).easing(cc.easeBackIn()),
                    cc.callFunc((self)=>{
                        let rpm = self.parent.parent.parent.parent.parent.parent.getComponent('RecipeMenu')
                        rpm.loadReputationBuyList(rpm.rightOrder +1)
                        rpm.checkButtonState(rpm)
                    },this),
                    cc.spawn(
                        cc.scaleTo(0.4,1,1).easing(cc.easeBackOut()),
                        cc.sequence(
                            cc.rotateTo(0.2,-10).easing(cc.easeSineOut()),
                            cc.rotateTo(0.1,10).easing(cc.easeSineInOut()),
                            cc.rotateTo(0.1,0).easing(cc.easeSineIn())
                        )
                    )
                ))
            }
        }
    }
    // update (dt) {},
    
});
